package 多客户端上传文件到一个服务器;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		ServerSocket ss=new ServerSocket(9099);
		while(true){
			Socket s = ss.accept();
			Server ser=new Server(s);
			Thread t=new Thread(ser);
			t.start();
		}
	}

}

class Server implements Runnable {
	Socket s;

	public Server(Socket s) {
		this.s = s;
	}

	public void run() {
		try {
			BufferedInputStream bis = new BufferedInputStream(
					s.getInputStream());
			BufferedOutputStream bos = new BufferedOutputStream(
					new FileOutputStream("a.txt"));
			byte[] data = new byte[1024];
			int read;
			while ((read = bis.read(data)) != -1) {
				bos.write(data, 0, read);
				bos.flush();
			}
			bos.close();
			System.out.println("请输入服务器反馈：");
			BufferedReader br = new BufferedReader(new InputStreamReader(
					System.in));
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
					s.getOutputStream()));
			String string = br.readLine();
			bw.write(string);
			bw.flush();
			s.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}